<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}

if($_POST['sub']!=''){


    $id=empty($_POST['id'])?0:(int)$_POST['id'];
	unset($_POST['id']);
	unset($_POST['sub']);
    
    
    if($id>0){
        $catid=$ezhand->select("thread",'catid','id="'.$id.'"');
    }else{
        $catid=intval($_POST["catid"]);
    }
    
    $categoryinfo=$ezhand->select("category",'*'," id=".$catid);
    if(empty($categoryinfo)){
       jumpajax('300','栏目不存在!',''); 
    }
    $catename=$categoryinfo["catname"];
    $modelid=$categoryinfo["modelid"];
    $workflowid=$categoryinfo["workflowid"];
    
    if(trim($_POST["title"])==""){
        
        jumpajax('300','标题不能为空!',''); 
    }
    $title=htmlspecialchars($_POST["title"],ENT_QUOTES);
    $keywords=htmlspecialchars($_POST["keywords"],ENT_QUOTES);
    $aid=intval($_POST["attachment_id"]);
    $threadinfo=array(
    "catid"=>$catid,
    "title"=>$title,
    "thumb"=>$_POST["attachment_fileName"],
    "keywords"=>$keywords,
    "uid"=>$_SESSION['EZ_Admin']['id'],    
    "username"=>$_SESSION['EZ_Admin']['name'],
    "updatetime"=>time(),
    "islink"=>0,
    "url"=>"",
    "ip"=>get_client_ip()
    );
    if($modelid==0){
        $tablecontent="pages";
        $data=array(
        'title'=>$title,
        'catid'=>$catid,
        'content'=>trim($_POST["content"])
        );
    }else{
        
        
    }

 	if($id==0){
        $threadinfo["inputtime"]=time();
        if($workflowid==0){
            $threadinfo["status"]=99;
        }else{
            $threadinfo["status"]=1;
        }
        $tid=$ezhand->insert("thread",$threadinfo);
        if(!empty($aid)){
            $attlist=array("tid"=>$tid,
            "aid"=>$aid
            );
            $ezhand->insert("attachment_index",$attlist);
        }
        $data["tid"]=$tid;
        
	    $pid=$ezhand->insert($tablecontent,$data);
		jumpajax('200','保存成功','index.php?mod=content&act=list&catid='.$catid,'');
	}
	else{
	    $tid=$id;
        if(!empty($aid)){
            $ezhand->delete('attachment_index','tid="'.$tid.'"');
            $attlist=array("tid"=>$tid,
            "aid"=>$aid
            );
            $ezhand->insert("attachment_index",$attlist);
        }	   
        $ezhand->update("thread",$threadinfo,'id="'.$id.'"');
        
        $data["tid"]=$id;
	    $ezhand->update($tablecontent,$data,'tid="'.$id.'"');
		jumpajax('200','修改成功','index.php?mod=content&act=list&catid='.$catid,'');
	}
}
else{
	$id=empty($_GET['id'])?0:(int)$_GET['id'];
    if($id==0){
	    $row=array();
		$role_menu_arr=array();
	}
	else{
	    $row=$ezhand->select(MOD,'*','id="'.$id.'"');
	}
}
?>